import { dmDB, kingbaseDB } from '../../utils/database.js';

// 从达梦数据库查询数据
export const queryDmData = async (ctx) => {
  try {
    const sql = 'SELECT * FROM your_table WHERE condition = ?';
    const params = [ctx.query.condition];
    const result = await dmDB.query(sql, params);
    ctx.success(result);
  } catch (err) {
    ctx.error('查询达梦数据库失败：' + err.message);
  }
};

// 向人大金仓数据库插入数据
export const insertKingbaseData = async (ctx) => {
  try {
    const { name, value } = ctx.request.body;
    const sql = 'INSERT INTO your_table (name, value) VALUES ($1, $2)';
    const params = [name, value];
    const result = await kingbaseDB.execute(sql, params);
    ctx.success(result);
  } catch (err) {
    ctx.error('插入金仓数据库失败：' + err.message);
  }
};

// 使用事务示例
export const transferData = async (ctx) => {
  try {
    await dmDB.transaction(async (connection) => {
      // 执行一系列操作
      await connection.execute('UPDATE table1 SET value = value - ? WHERE id = ?', [100, 1]);
      await connection.execute('UPDATE table2 SET value = value + ? WHERE id = ?', [100, 2]);
    });
    ctx.success(null, '转账成功');
  } catch (err) {
    ctx.error('事务执行失败：' + err.message);
  }
}; 